﻿using IServices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Services;
using Models;
using Models.Dtos;
using Common;
using Microsoft.AspNetCore.Authorization;
using Common.AuthHelper;

namespace SqlSugarDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        readonly IUserServices _userServices;
        public UserController(IUserServices services)
        {
            this._userServices = services;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        [Authorize(Roles ="Admin")]
        [Route("InsertUser"),HttpPost]
        public async Task<IActionResult> InsertUser([FromBody] UserDto Info)
        {
            //var result =  await _userServices.UserInsertAsync(Info);
            var token = Request.Headers["Authorization"].ToString().Replace("Bearer ","");

             var jwt =  JwtHelper.GetTokenValue(token);

            return Ok(jwt);
             
            //我想拿到我token 的信息

        }
        [Authorize]
        [Route("InsertUser1"), HttpPost]
        public async Task<IActionResult> InsertUser1([FromBody] UserDto Info)
        {
            var result = await _userServices.UserInsertAsync(Info);
            return Ok(result);
        }


        [Authorize(Roles ="Teacher,Admin")]
        [Route("InsertUser2"), HttpPost]
        public async Task<IActionResult> InsertUser2([FromBody] UserDto Info)
        {
            var result = await _userServices.UserInsertAsync(Info);
            return Ok(result);
        }





    }
}
